import { v4 as uuid } from 'uuid'
import { StdTextDataV2, StdTextsV2 } from './textData'

export class StdNote {
  constructor({ dataId, shareId, referredId, contentNumber, dataLang = 'zh', textData, dataType, isRevised = false }) {
    // 数据唯一标识
    this.dataId = dataId || uuid()
    // 中英文共享id-中文的单个注和英文的单个注进行联动
    this.shareId = shareId
    // 被引用id-脚注场景使用-存放shareId,因为一个脚注可能对应中文和英文两条注内容
    this.referredId = referredId
    // 注编号 - 前端预留
    this.contentNumber = contentNumber
    // 中英文区分 zh:中文，en:英文
    this.dataLang = dataLang
    // 内容数组 - 目前不支持在表注中再添加脚注，所以下方contentType没有footnote类型
    // 具体内容类型contentType = text（默认文本) sup(上标) sub（下标）
    this.textData = new StdTextDataV2(textData)
    // 数据类型
    this.dataType = dataType
  }
}

export class StdNoteElementV2 {
  constructor(node) {
    this.node = node
  }
  get data() {
    return this.parseNote(this.node)
  }
  parseNote(node) {
    const attrMap = node.attributes?.map || {}
    return {
      dataId: attrMap['data-id'],
      shareId: attrMap['data-share-id'],
      textData: new StdNoteTextDataV2(node).data
    }
  }
}

export class StdNoteTextDataV2 extends StdTextsV2 {
  constructor(node, contentType = '') {
    super(node)
    this.contentType = contentType
    this.contentList = []
  }
  get data() {
    this.getNumberContent(this.node)
    this.getContent(this.node?.firstChild)
    return {
      dataId: this.node?.dataset?.id,
      contentList: this.contentList
    }
  }
  // 拼接列项前面的编号
  getNumberContent(node) {
    this.contentList.push({
      dataLang: node.dataset?.lang,
      // 脚注的编号，type传sup
      contentType: this.contentType.includes('footnote') ? 'sup' : 'text',
      contentNumber: node?.dataset?.number || null,
      content: '',
      fullContent: this.getFullNumber(node)
    })
  }
  getFullNumber(node) {
    const typeMapZh = {
      example: '示例',
      item_note: '注',
      figure_note: '注',
      figure_footnote: '',
      footnote: '',
      table_note: '注'
    }
    const typeMapEn = {
      example: 'Example',
      item_note: 'Note',
      figure_note: 'Note',
      figure_footnote: '',
      footnote: '',
      table_note: 'Note'
    }
    const attrs = node.dataset
    const dataType = attrs?.type
    const dataNumber = attrs?.number || ''
    const types = node?.dataset?.lang === 'en' ? typeMapEn : typeMapZh
    const colon = node?.dataset?.lang === 'en' ? ':' : '：'
    return `${types[dataType] || ''}${dataNumber}${!dataType?.includes?.('footnote') ? colon : ''} `
  }
}
